<?php

namespace common\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Goods;

/**
 * GoodsSearch represents the model behind the search form about `common\models\Goods`.
 */
class GoodsSearch extends Goods
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'num', 'sum', 'look', 'create_at', 'uid'], 'integer'],
            [['price','name', 'logo', 'stuff', 'pid', 'sheath', 'grind', 'craftwork', 'gift', 'message'], 'safe'],
            [[ 'sumlen', 'bladelen', 'stemlen', 'width', 'height', 'hard', 'weight'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Goods::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'num' => $this->num,
            'sum' => $this->sum,
            'look' => $this->look,
            'sumlen' => $this->sumlen,
            'bladelen' => $this->bladelen,
            'stemlen' => $this->stemlen,
            'width' => $this->width,
            'height' => $this->height,
            'hard' => $this->hard,
            'weight' => $this->weight,
            'create_at' => $this->create_at,
            'uid' => $this->uid,
        ]);
        if(isset($this->price)){
            if(!empty($this->price)){
                if(isset($this->price[0]) && isset($this->price[1]) && $this->price[0]!='' && $this->price[1]!=''){
                    $query->andWhere(['between','price',$this->price[0],$this->price[1]]);
                }elseif(isset($this->price[0]) && $this->price[0]!=''){
                    $query->andWhere(['>','price',$this->price[0],]);
                }elseif(isset($this->price[1]) && $this->price[1]!='' ){
                    $query->andWhere(['<','price',$this->price[1]]);
                }
            }
        }
        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['in', 'pid', $this->pid]);

        return $dataProvider;
    }
}
